﻿using System.Web.Mvc;
using System.Linq;
using MvcMall.AdminPortal.Controllers.Store.ViewModels;
using MvcMall.Core.Contacts.Tasks;
using MvcMall.Core.Enums;
using MvcMall.Framework.Extensions;
using MvcMall.Framework.Mapper;
using SharpArch.Web.NHibernate;
using MvcMall.Framework.MvcPager;

namespace MvcMall.AdminPortal.Controllers.Store
{
    public class StoreController : BaseController
    {
        private readonly IUserTask _userTask;

        private readonly IStoreTask _storeTask;

        private readonly IMapper<Core.Domains.Store, StoreModel> _storeModelMapper;

        public StoreController(IUserTask userTask, IStoreTask iStoreTask, IMapper<Core.Domains.Store, StoreModel> storeModelMapper)
        {
            _userTask = userTask;
            _storeTask = iStoreTask;
            _storeModelMapper = storeModelMapper;
        }

        public ActionResult StoreVerify(int? pageIndex)
        {
            const int pageSize = 10;
            var list = _storeTask.GetDsPagedList(pageIndex ?? 1, pageSize);
            var model = list.Select(x => _storeModelMapper.MapFrom(x)).ToPagedList(pageIndex ?? 1, pageSize, list.TotalItemCount);
            return View(model);
        }

        public ActionResult StoreDetails(int storeId)
        {

            return View();
        }


        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult StoreDetails(StoreModel model)
        {
            return View();
        }

        public ActionResult StoreList()
        {
            var storeTypeList = StoreType.个人全职.ToSelectList();
            storeTypeList.Add(new SelectListItem
            {
                Text = "全部",
                Selected = true,
                Value = "",
            });
            ViewData["StoreTypeList"] = storeTypeList;

            var toreStatusList = StoreStatus.未审核.ToSelectList();
            toreStatusList.Add(new SelectListItem
            {
                Text = "全部",
                Selected = true,
                Value = "",
            });
            ViewData["StoreStatusList"] = toreStatusList;

            const int pageIndex = 1;
            const int pageSize = 1;
            var list = _storeTask.GetPagedList(pageIndex, pageSize, string.Empty, string.Empty, null, null, string.Empty);
            var pagedList = list.Select(_storeModelMapper.MapFrom).ToPagedList(pageIndex, pageSize,
                                                                           list.TotalItemCount);
            var model = new StoreListSearchModel
                            {
                                Stores = pagedList
                            };
            return View(model);
        }

        [HttpPost]
        public ActionResult StoreList(int? pageIndex, StoreListSearchModel model)
        {
            var storeTypeList = StoreType.个人全职.ToSelectList();
            storeTypeList.Add(new SelectListItem
                                  {
                                      Text = "全部",
                                      Selected = true,
                                      Value = "",
                                  });
            ViewData["StoreTypeList"] = storeTypeList;

            var toreStatusList = StoreStatus.未审核.ToSelectList();
            toreStatusList.Add(new SelectListItem
                                   {
                                       Text = "全部",
                                       Selected = true,
                                       Value = "",
                                   });
            ViewData["StoreStatusList"] = toreStatusList;

            const int pageSize = 1;

            var list = _storeTask.GetPagedList(pageIndex ?? 1
                                               , pageSize
                                               , model.UserName ?? string.Empty
                                               , model.PromoteUser ?? string.Empty
                                               , model.StoreStatus == null ? (StoreStatus?)null : (StoreStatus)model.StoreStatus
                                               , model.StoreType == null ? (StoreType?)null : (StoreType)model.StoreType
                                               , model.StoreName ?? string.Empty);
            var pagedList = list.Select(_storeModelMapper.MapFrom).ToPagedList(pageIndex ?? 1, pageSize,
                                                                           list.TotalItemCount);
            model.Stores = pagedList;
            return View(model);

        }

        public ActionResult StorePass()
        {
            return View("StoreVerify");
        }
    }
}
